﻿
using Models.Common;
using Models.Device;

using UtilsBase;
using Microsoft.Extensions.FileSystemGlobbing.Internal;
using System.Text.RegularExpressions;
using System.Data;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    /// GPS,北斗定位
    /// </summary>
    public class ServiceDeviceRS4851043_GPS_BEIDOU
    {

        //回调获获状态,为了避免误差，查状态和执行，都返回了 当前开关状态
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;
                string result = modelDeviceData.commandResults[0].value;

                Console.WriteLine("-------GPS--------:" + result.ToString());
                if (commandLogModel.key == "latitude")
                {//ieee754转换为十进制,gps定位结果解析用到
                    //01 03 04 41 EC 5F 6F 56 26 维度 41 EC 5F 6F=当前纬度：29.5466°
                    string dataHex = result.Substring(9, 11);
                    float data = UtilsDataTypeChange.IEEE754_Int32BitsToSingle(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "latitude", data.ToString(), "维度", result);
                }
                else if (commandLogModel.key == "longitude")
                {//ieee754转换为十进制,gps定位结果解析用到
                    //01 03 00 9A 00 02 E4 24
                    string dataHex = result.Substring(9, 11);
                    float data = UtilsDataTypeChange.IEEE754_Int32BitsToSingle(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "longitude", data.ToString(), "经度", result);
                }
                else if (commandLogModel.key == "longitudeandlatitude")
                {//ieee754转换为十进制,gps定位结果解析用到
                    //01 03 08 00 00 00 00 00 00 00 00 95 D7
                    string dataHex = result.Substring(9, 11);
                    float data = UtilsDataTypeChange.IEEE754_Int32BitsToSingle(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "latitude", data.ToString(), "纬度", result);

                    dataHex = result.Substring(21, 11);
                    data = UtilsDataTypeChange.IEEE754_Int32BitsToSingle(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "longitude", data.ToString(), "经度", result);
                }
                else if (commandLogModel.key == "altitude")
                {
                    string dataHex = result.Substring(9, 11);
                    float data = UtilsDataTypeChange.IEEE754_Int32BitsToSingle(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "altitude", UtilsNumber.GetNumberForLength(data, 1).ToString(), "海拔", result);
                }
                else if (commandLogModel.key == "speed")
                {
                    string dataHex = result.Substring(9, 11);
                    float data = UtilsDataTypeChange.IEEE754_Int32BitsToSingle(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "speed", UtilsNumber.GetNumberForLength(data, 1).ToString(), "速度", result);
                }
                else if (commandLogModel.key == "year")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "year", data.ToString(), "年", result);
                }
                else if (commandLogModel.key == "month")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "month", data.ToString(), "月", result);
                }
                else if (commandLogModel.key == "date")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "date", data.ToString(), "日期", result);
                }
                else if (commandLogModel.key == "hour")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "hour", data.ToString(), "小时", result);
                }
                else if (commandLogModel.key == "second")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "second", data.ToString(), "秒", result);
                }
                else if (commandLogModel.key == "minute")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "minute", data.ToString(), "分", result);
                }
                else if (commandLogModel.key == "status")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "status", data.ToString(), "状态", result);
                }
                else if (commandLogModel.key == "viewSatellite")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "viewSatellite", data.ToString(), "可视卫星", result);
                }
                else if (commandLogModel.key == "useSatellite")
                {
                    //01 03 02 00 00 B8 44
                    string dataHex = result.Substring(9, 5);
                    double data = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    log.writeSensorlog(commandLogModel.d_guid, "useSatellite", data.ToString(), "使用卫星", result);
                }
                else if (commandLogModel.key == "dtu_lbsgps")
                {
                    //+GSP: Lac:0x7521,CellId:0x7514903\r\n\r\nOK\r\n
                    //(?<=Lac:).*(?=,)
                    //0x7521

                    //(?<=CellId:).*(?<=OK)
                    //0x7514903\r\n\r\nOK

                    string input = result;//.Substring(9, 5);

                    // 获取所有匹配
                    MatchCollection matches = Regex.Matches(input, @"(?<=Lac:).*(?=,)");
                    foreach (Match match in matches)
                    {
                        string data = match.Value.Replace("0x", "");

                        log.writeSensorlog(commandLogModel.d_guid, "lac", data, "基站区域码", data);

                        ///Console.WriteLine("Found 'ai' at position " + match.Index);
                    }

                    matches = Regex.Matches(input, @"(?<=CellId:).*");
                    foreach (Match match in matches)
                    {
                        string data = match.Value.Replace("0x", "");
                        data = data.Replace("OK", "");
                        data = data.Replace("\r\n", "");
                        data = data.Replace("\r", "");
                        log.writeSensorlog(commandLogModel.d_guid, "cellid", data, "基站码", data);
                        ///Console.WriteLine("Found 'ai' at position " + match.Index);
                    }


                }
                else if (commandLogModel.key == "dtu_bjtime")
                {
                    //+TIME: 24/07/12,16:48:07\r\nOK\r\n
                    //(?<=TIME:).*                

                    string input = result;//.Substring(9, 5);

                    // 获取所有匹配
                    MatchCollection matches = Regex.Matches(input, @"(?<=TIME:).*");
                    foreach (Match match in matches)
                    {
                        string data = match.Value.Replace("0x", "");
                        data = data.Replace("\r\n", "");
                        data = data.Replace("\r", "");
                        log.writeSensorlog(commandLogModel.d_guid, "bjtime", data, "北京时间", data);

                        ///Console.WriteLine("Found 'ai' at position " + match.Index);
                    }
                }
                else if (commandLogModel.key == "dtu_timestamp")
                {
                    //+TIME: 24/07/12,16:48:07\r\nOK\r\n
                    //(?<=TIME:).*                

                    string input = result;//.Substring(9, 5);

                    // 获取所有匹配
                    MatchCollection matches = Regex.Matches(input, @"(?<=TIMESTAMP:).*");
                    foreach (Match match in matches)
                    {
                        string data = match.Value.Replace("0x", "");
                        data = data.Replace("\r\n", "");
                        data = data.Replace("\r", "");
                        log.writeSensorlog(commandLogModel.d_guid, "timestamp", data, "时间戳", data);

                        ///Console.WriteLine("Found 'ai' at position " + match.Index);
                    }
                }
                else if (commandLogModel.key == "dtu_utctime")
                {
                    //+TIME: 24/07/12,16:48:07\r\nOK\r\n
                    //(?<=TIME:).*                

                    string input = result;//.Substring(9, 5);

                    // 获取所有匹配
                    MatchCollection matches = Regex.Matches(input, @"(?<=TIME:).*");
                    foreach (Match match in matches)
                    {
                        string data = match.Value.Replace("0x", "");
                        data = data.Replace("\r\n", "");
                        data = data.Replace("\r", "");
                        log.writeSensorlog(commandLogModel.d_guid, "utctime", data, "UTC时间", data);

                        ///Console.WriteLine("Found 'ai' at position " + match.Index);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
